package rs.mbs2.epics.server.servlet;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import rs.mbs2.epics.server.entity.Photo;
import rs.mbs2.epics.server.entity.Tag;
import rs.mbs2.epics.server.servlet.util.Parse;
import rs.mbs2.epics.server.servlet.util.Validation;
import rs.mbs2.epics.server.session.dao.localinterface.PhotoDaoLocal;
import rs.mbs2.epics.server.session.dao.localinterface.TagDaoLocal;

@SuppressWarnings("serial")
public class SearchPhotosController extends HttpServlet {

	@EJB
	private PhotoDaoLocal photoDao;

	@EJB
	private TagDaoLocal tagDao;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		System.out.println("SearchPhotosController.doGet()");


		String title = request.getParameter("title").trim();
		String description = request.getParameter("description").trim();
		String firstname = request.getParameter("name").trim();
		String lastname = request.getParameter("lastName").trim();
		String tags = request.getParameter("tags").trim();
		String widthString = request.getParameter("width").trim();
		String heightString = request.getParameter("height").trim();
		boolean widthGreater = request.getParameter("widthGreater") != null;
		boolean heightGreater = request.getParameter("heightGreater") != null;
		String averageRatingString = request.getParameter("rating").trim();

		Set<Photo> results = new HashSet<>();
		boolean isValid = true;

		isValid = Validation.validateString(title);
		if(isValid) {
			List<Photo> photos = photoDao.findPhotosByTitle(title);
			if(photos != null) {
				for(Photo photo : photos)
					if(photo.getPubliclyAvailable())
						results.add(photo);
			}
		}

		isValid = Validation.validateString(description);
		if(isValid) {
			List<Photo> photos = photoDao.findPhotosByDescription(description);
			if(photos != null) {
				for(Photo photo : photos) {

					if(photo.getPubliclyAvailable())
						results.add(photo);
				}
			}
		}

		isValid = Validation.validateString(firstname);
		isValid = isValid || Validation.validateString(lastname);	// dovoljno je da je popunjeno bar jedno polje (ili ime ili prezime) pa da se opali upit u bazu
		if(isValid) {
			List<Photo> photos = photoDao.findPhotosByNameAndLastname(firstname, lastname);
			if(photos != null) {
				for(Photo photo : photos)
					if(photo.getPubliclyAvailable())
						results.add(photo);
			}
		}


		isValid = Validation.validateString(tags);
		if(isValid) {
			Set<String> tagsStringSet = Parse.parseCommaSeparatedStrings(tags);
			Set<Tag> tagsSet = new HashSet<>();
			for(String tagString : tagsStringSet) {
				List<Tag> tagsFound = tagDao.findTagByText(tagString);
				for(Tag tag : tagsFound)
					tagsSet.add(tag);
			}

			for(Tag tag : tagsSet) {
				List<Photo> photos = photoDao.findPhotosByTags(tag);
				if(photos != null) {
					for(Photo photo : photos)
						if(photo.getPubliclyAvailable())
							results.add(photo);
				}
			}
		}

		isValid = Validation.validateString(averageRatingString);
		if(isValid) {
			Double averageRating = Double.parseDouble(averageRatingString);

			List<Photo> photos = photoDao.findAll();
			for(Photo photo : photos) {
				if(photo.getPubliclyAvailable()) {
					if(photo.getAverageRating().equals(averageRating)) {
						results.add(photo);
					}
				}
			}
		}

		isValid = Validation.validateString(widthString);
		if(isValid) {
			Integer width = Integer.parseInt(widthString);

			List<Photo> photos = photoDao.findPhotosByWidth(width, widthGreater);
			if(photos != null) {
				for(Photo photo : photos)
					if(photo.getPubliclyAvailable())
						results.add(photo);
			}
		}

		isValid = Validation.validateString(heightString);
		if(isValid) {
			Integer height = Integer.parseInt(widthString);

			List<Photo> photos = photoDao.findPhotosByHeight(height, heightGreater);
			if(photos != null) {
				for(Photo photo : photos)
					if(photo.getPubliclyAvailable())
						results.add(photo);
			}
		}

		for(Photo photo : results) {
			System.out.println("Slika: " + photo.getTitle());
			if(photo.getDescription().length() > 200) {
				String limitedDescription = photo.getDescription().substring(0, 200);
				photo.setDescription(limitedDescription);
			}
		}

		request.setAttribute("resultSet", results);
		getServletContext().getRequestDispatcher("/searchResults.jsp").forward(request, response);

	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
